package com.fly.dbexample;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.fly.db.FlySQLiteOpenHelper;
import com.fly.db.entity.DBEntity;
import com.fly.db.entity.Table;
import com.fly.db.exception.DBInitException;

/**
 * Created with Android Studio.
 * Package_Name：com.fly.dbexample
 * Project_Name：DBExample
 * User：郭鹏飞
 * Date：2016/1/19
 * Email：love518420@foxmail.com
 * Description：
 */
public class FLYApplication extends Application {

    private static FLYApplication instance = null;

    private FlySQLiteOpenHelper flySQLiteOpenHelper;

    @Override
    public void onCreate() {
        super.onCreate();

        if (instance == null) {
            instance = this;
        }

        DBEntity dbEntity = new DBEntity();
        Table table = new Table("user");
        table.put("name", "char");
        table.put("age", "integer");
        dbEntity.add(table);
        try {
            FlySQLiteOpenHelper.initDB(this);
            flySQLiteOpenHelper = FlySQLiteOpenHelper.getInstance(this, dbEntity);
            flySQLiteOpenHelper.setOnUpgradeListener(new FlySQLiteOpenHelper.OnUpgradeListener() {
                @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                    Log.e("tag", oldVersion + "-" + newVersion);
                }
            });
        } catch (DBInitException e) {
            e.printStackTrace();
        }

    }

    public static FLYApplication getInstance() {
        return instance;
    }

    public FlySQLiteOpenHelper getFlySQLiteOpenHelper() {
        return flySQLiteOpenHelper;
    }

    public SQLiteDatabase openDatabase() {
        return flySQLiteOpenHelper.openDatabase();
    }

    public void closeDatabase() {
        flySQLiteOpenHelper.closeDatabase();
    }

}
